package com.xuxin.baby.adapter;

import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.ColorUtils;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.SpanUtils;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.xuxin.baby.R;
import com.xuxin.baby.bean.OrderBean;
import com.xuxin.baby.glide.GlideApp;
import com.xuxin.baby.network.Retrofit;
import com.xuxin.baby.utils.BaseUtils;
import com.xuxin.baby.utils.RecycleUtils;

import org.jetbrains.annotations.NotNull;

/**
 * Created by xu_xin
 * Created on 2020/11/6 0006
 * 我的订单
 */
public class UserOrderAdapter extends BaseQuickAdapter<OrderBean.ResultBean.ItemsBean, BaseViewHolder> {

    public UserOrderAdapter() {
        super(R.layout.item_user_order);
    }

    @Override
    protected void convert(@NotNull BaseViewHolder holder, OrderBean.ResultBean.ItemsBean item) {
        holder.setText(R.id.item_user_order_title, "订单编号 : " + item.getOrderNo());
        holder.setText(R.id.item_user_order_time, "创建时间 : " + BaseUtils.getTime(item.getCreationTime()));
        holder.setText(R.id.item_user_order_status, item.getStatus());
        TextView item_user_order_total = holder.getView(R.id.item_user_order_total);
        SpanUtils.with(item_user_order_total).append("总计 : ").append(item.getTotalBeans() + "豆").setForegroundColor(ColorUtils.getColor(R.color.colorOrange)).create();
        RecyclerView item_user_order_recycle = holder.getView(R.id.item_user_order_recycle);
        RecycleUtils.init(item_user_order_recycle);
        UserOrderChildAdapter myAdapter = new UserOrderChildAdapter();
        item_user_order_recycle.setAdapter(myAdapter);
        myAdapter.setList(item.getDetails());
    }

    public static class UserOrderChildAdapter extends BaseQuickAdapter<OrderBean.ResultBean.ItemsBean.DetailsBean, BaseViewHolder> {
        RequestOptions options = new RequestOptions().transform(new CenterCrop(), new RoundedCorners(5));

        public UserOrderChildAdapter() {
            super(R.layout.item_user_order_child);
        }

        @Override
        protected void convert(@NotNull BaseViewHolder holder, OrderBean.ResultBean.ItemsBean.DetailsBean itemsBean) {
            if (null != itemsBean.gettCourse()) {
                holder.setText(R.id.item_user_order_child_title, itemsBean.gettCourse().getTitle());
                holder.setText(R.id.item_user_order_child_price, itemsBean.getUnitBean() + "豆");
                /**
                 * 加载图片
                 */
                ImageView item_user_order_child_image = holder.getView(R.id.item_user_order_child_image);
                String orderImage = Retrofit.image_url + itemsBean.gettCourse().getImageUrl();
                LogUtils.e("我的订单_image = " + orderImage);
                GlideApp.with(getContext()).load(orderImage).placeholder(R.mipmap.img_placeholder260).apply(options).into(item_user_order_child_image);
            }
        }
    }
}